package com.smssalama.gui.setup;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import com.smssalama.AlertManager;
import com.smssalama.SMSSalama;
import com.smssalama.SMSSalamaSession;
import com.smssalama.UserProfile;
import com.smssalama.i18n.CommandLabels;
import com.smssalama.i18n.ErrorMessages;
import com.smssalama.i18n.Labels;
import com.smssalama.i18n.LocalizableFactory;
import com.smssalama.i18n.MessageConstants;
import com.smssalama.i18n.ScreenTitles;

/**
 * Prompts the user for setup info
 * 
 * @author Arnold P. Minde
 */
public class Setup extends Form implements CommandListener {
	public class ChangeLocaleCmd extends Command {
		private final String locale;

		public ChangeLocaleCmd(String locale, String displayName, int priority) {
			super(displayName, Command.SCREEN, priority);
			this.locale = locale;
		}

		public String getLocale() {
			return this.locale;
		}
	}

	TextField password1 = new TextField("", "", 15, TextField.ANY);
	TextField password2 = new TextField("", "", 15, TextField.ANY);
	StringItem intro = new StringItem("", "");

	public Setup() {
		super("");
		System.out.println("Starting ...");
		Object o = LocalizableFactory.createLocalizedObject(ScreenTitles.class);
		System.out.println("Starting ... "+o.getClass().getName());
		final ScreenTitles screenTitles = (ScreenTitles) LocalizableFactory.createLocalizedObject(ScreenTitles.class);
		System.out.println("Starting ...");
		this.setTitle(screenTitles.setup());
		// Set up this Displayable to listen to command events
		setCommandListener(this);
		// add the Exit command
		final CommandLabels commandLabels = (CommandLabels) LocalizableFactory
				.createLocalizedObject(CommandLabels.class);
		addCommand(new Command(commandLabels.ok(), Command.OK, 1));
		/*
		String[][] locales = Utils.getSupportedLocales();
		for (int i = 0; i < locales.length; i++) {
			String[] locale = locales[i];
			ChangeLocaleCmd cmd = new ChangeLocaleCmd(locale[0], locale[1], 3);
			this.addCommand(cmd);
		}
		*/

		this.append(this.intro);

		final Labels labels = (Labels) LocalizableFactory
				.createLocalizedObject(Labels.class);
		this.password1.setLabel(labels.password());
		this.password1.setConstraints(TextField.ANY | TextField.PASSWORD);
		this.password1.setMaxSize(30);
		this.append(this.password1);

		this.password2.setLabel(labels.confirmPassword());
		this.password2.setConstraints(TextField.ANY | TextField.PASSWORD);
		this.password2.setMaxSize(30);
		this.append(this.password2);

		final MessageConstants messages = (MessageConstants) LocalizableFactory
				.createLocalizedObject(MessageConstants.class);
		this.intro.setText(messages.setupIntro());

		// intro.setPreferredSize();
	}

	public void commandAction(Command command, Displayable displayable) {
		if (command instanceof Setup.ChangeLocaleCmd) {
			Setup.ChangeLocaleCmd cmd = (Setup.ChangeLocaleCmd) command;
			SMSSalamaSession.getSession().setLocale(cmd.getLocale());
			Setup setup = (Setup) LocalizableFactory.createLocalizedObject(Setup.class);
			AlertManager.showDisplayable(setup);
		}else if (command.getCommandType() == Command.OK) {
			// perform setup
			try {
				performSetup();
			} catch (Exception ex) {
				AlertManager.showError(this, ex);
			}
		}
	}

	private void performSetup() throws Exception {
		String p1 = this.password1.getString();
		String p2 = this.password2.getString();
		if (p1.equals(p2)) {
			UserProfile s = UserProfile.initialize(p1);
			// Profile p = new Profile("default", p1);
			// Profile.addProfile(p);
			if(UserProfile.initialized()){
				SMSSalama.promptAuthentication(null);
			}
		} else {
			final ErrorMessages errorMessages = (ErrorMessages) LocalizableFactory
					.createLocalizedObject(ErrorMessages.class);
			throw new Exception(errorMessages.passwordsDoNotMatch());
		}
	}
}
